#-*- coding: utf8 -*-
import chips.jsonlib.types
import chips.jsonlib.types.ext
from chips.jsonlib.schemas import *

class ValueExt(Schema):
    ext_type = None

    def __init__(self, *args, **kwds):
        super(ValueExt, self).__init__(*args, **kwds)
        self.ext_type_type = self.ext_type.type
        self.schema = self.ext_type.schema

    def validation_object(self, obj, **kwds):
        return isinstance(obj, self.ext_type_type)

    def _validation_function(self, obj, **kwds):
        '''
        Validate ValueExt objects. Users should override the 'validation_object'
        method. This method is always called with the Python representation of 
        the ValueExt object. If the argument is the JSON representation of this
        object, it will get converted and validated.
        '''
        # Validate object 
        if isinstance(obj, self.ext_type):
            return self.validation_object(obj, **kwds)

        # Validate JSON object
        if self.schema.is_valid(obj):
            obj = self.ext_type.from_json(obj)
            return self.validation_object(obj, **kwds)
        else:
            return False

# Export all Schemas
__all__ = []
for k, v in globals().items():
    if isinstance(v, type) and issubclass(v, Schema) and not k.startswith('_'):
        __all__.append(k)

if __name__ == '__main__':
    import doctest
    doctest.testmod(optionflags=doctest.REPORT_ONLY_FIRST_FAILURE, verbose=0)
